package adminAction;

import com.google.gson.Gson;
import dao.BookDao;
import po.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;

@WebServlet(urlPatterns = "/admin/adminAction/AllProductServlet")
public class AllProductServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int draw = Integer.parseInt(req.getParameter("draw"));
        int start = Integer.parseInt(req.getParameter("start"));
        int length = Integer.parseInt(req.getParameter("length"));
        //调用dao层方法，将数据库order表中所有信息查出来
        BookDao bookDao = new BookDao();
        int recordsTotal = bookDao.selectCount();
        ArrayList<Book> books = new ArrayList<Book>();
        books = bookDao.selectAllPaged(start,length);
        //绑定数据
        HashMap<String,Object> valude = new HashMap<String, Object>();
        valude.put("draw",draw);
        valude.put("recordsTotal",recordsTotal);
        valude.put("recordsFiltered",recordsTotal);
        valude.put("data",books);
        Gson gson = new Gson();
        String jsonstr = gson.toJson(valude);
        Writer out = null;
        try {
            out = resp.getWriter();
            out.write(jsonstr);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //响应浏览器
    }
}
